package com.diandiansong.app.ui.cate;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.diandiansong.app.R;
import com.diandiansong.app.entity.CommodityBrainPowerInfo;
import com.diandiansong.app.ui.base.BaseAct;
import com.diandiansong.app.utils.A;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import diandiansong.library.utils.CCallBack;
import diandiansong.library.widgets.FlowLayout;

/**
 * 筛选条件
 */
public class Filter extends BaseAct {

    public static String sBand;
    public static String sKind;

    static String mCateId;

    public static void start(Context context, String cateId) {
        mCateId = cateId;
        Intent starter = new Intent(context, Filter.class);
        context.startActivity(starter);
    }

    @BindView(R.id.fl_band)
    FlowLayout mFlBand;
    @BindView(R.id.fl_kind)
    FlowLayout mFlKind;

    View[] mBandViews;
    View[] mKindViews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addContentView(R.layout.cate_filter);
        hideTitle();

        onLoading();
        A.commodityBrain_power(mCateId, new CCallBack<CommodityBrainPowerInfo>() {
            @Override
            public void onSuccess(CommodityBrainPowerInfo o) throws Exception {
                onLoaded();

                List<CommodityBrainPowerInfo.Data.BrandInfo> brandInfo = o.getData().getBrandInfo();
                mBandViews = new View[brandInfo.size()];
                for (int i = 0; i < brandInfo.size(); i++) {
                    final CommodityBrainPowerInfo.Data.BrandInfo item = brandInfo.get(i);

                    View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.cate_filter_item, null);
                    itemView.setLayoutParams(new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT
                    )));
                    TextView tvName = itemView.findViewById(diandiansong.library.R.id.tv_name);
                    tvName.setText(item.getTypeName());
                    itemView.setTag(item.getTypeName());
                    if (sBand != null && sBand.equals(item.getTypeName())) {
                        tvName.setActivated(true);
                    }
                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (sBand != null && sBand.equals(item.getTypeName())) {
                                v.findViewById(R.id.tv_name).setActivated(false);
                                sBand = null;

                            } else {
                                sBand = item.getTypeName();

                                for (View skuView : mBandViews) {
                                    skuView.findViewById(R.id.tv_name).setActivated(false);
                                }
                                v.findViewById(R.id.tv_name).setActivated(true);
                            }

                            Cate.GoodList.doRefreshX(false);
                            finish();
                        }
                    });
                    mBandViews[i] = itemView;
                    mFlBand.addView(itemView);
                }

                List<CommodityBrainPowerInfo.Data.BreedInfo> breedInfo = o.getData().getBreedInfo();
                mKindViews = new View[breedInfo.size()];
                for (int i = 0; i < breedInfo.size(); i++) {
                    final CommodityBrainPowerInfo.Data.BreedInfo item = breedInfo.get(i);

                    View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.cate_filter_item, null);
                    itemView.setLayoutParams(new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT
                    )));
                    TextView tvName = itemView.findViewById(diandiansong.library.R.id.tv_name);
                    tvName.setText(item.getTypeName());
                    itemView.setTag(item.getTypeName());
                    if (sKind != null && sKind.equals(item.getTypeName())) {
                        tvName.setActivated(true);
                    }
                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (sKind != null && sKind.equals(item.getTypeName())) {
                                v.findViewById(R.id.tv_name).setActivated(false);
                                sKind = null;

                            } else {
                                sKind = item.getTypeName();

                                for (View skuView : mKindViews) {
                                    skuView.findViewById(R.id.tv_name).setActivated(false);
                                }
                                v.findViewById(R.id.tv_name).setActivated(true);
                            }

                            Cate.GoodList.doRefreshX(false);
                            finish();
                        }
                    });
                    mKindViews[i] = itemView;
                    mFlKind.addView(itemView);
                }

            }

            @Override
            public void onFailure(int code, String msg) {
                onLoaded();
                toast(msg);
            }
        });
    }


    @OnClick(R.id.do_close)
    public void onViewClicked() {
        Cate.GoodList.doRefreshX(false);
        finish();
    }

    @OnClick(R.id.blank)
    public void onBlankClicked() {
        Cate.GoodList.doRefreshX(false);
        finish();
    }
}
